;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var dialog = require("dialog");
	var leftMenu = require("../leftMenu/leftMenu");
	var topMenu = require("../topMenu/topMenu");
	var router = require("../hash/pageRouter");
	var footer = require("../footer/footer_main");
	var validate = require("validate");
	var _css = require("./bulkstockManage.css");
	var constant = require("constant");
	var html_edit_copy_bulkstock = require("./html_edit_copy_bulkstock.html");
	var databind = require("databind");
	var tools = require("tools");
	var i = 1;
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var imgSrc;
	var commodityCategoryIdDic = {
		enabled: 0,
		commodityCategoryId: 0
	};

	var init = function(num) {

		$(".table-section").removeClass("none");
		$("#project").removeClass("topMenu_selected");
		inquireBulkstockInfoPageList();
		initEvent();
		router.init();
	};

	var initEvent = function() {

		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}

		//搜索按钮
		$("#bulkstockManageSearchButton").on("click", function() {
			pageIndex = 1;
			inquireBulkstockInfoPageList();
		});
		
		//添加跳转
		$("#main").on("click", "#commodityPriceCollection", function() {
			router.route("commodityPriceCollection/commodityPriceCollectionList.html");
		});

		$("#main").on("click", "#commodityPriceChecked", function() {
			router.route("commodityPriceManage/commodityPriceChecked.html");
		});

		$("#main").on("click", "#commodityPriceUnchecked", function() {
			router.route("commodityPriceManage/commodityPriceUnchecked.html");
		});
		
		$("#bulkstockInfoAdd").unbind("click").click(function(){
			var ii = layer.open({
			  title: '新增大宗商品',
			  type: 1,
			  skin: 'layui-layer-rim', //加上边框
			  area: ['800px', '350px'], //宽高
			  content: html_edit_copy_bulkstock,
			  btn: ['确定', '取消'],
			  yes: function(){
			  	databind.submitDom($(".bulkstockInfoDialog"), function(){
			  		layer.msg("新增成功！");
			  		layer.close(ii);
			  	});
			  }
			});
			
			tools.loadSelectList($(".bulkstockInfoDialog select[name=bulkstockCategoryId]"), "inquireBulkstockCategoryPageList.json", "bulkstockCategoryId", "categoryName", null);
			tools.loadSelectList($(".bulkstockInfoDialog select[name=originalPlace]"), "inquireZoneProvinceList.json", "provinceNameCN", "provinceNameCN", null);
			tools.clearNoNumAll($(".bulkstockInfoDialog"));
		});
	};

	//查询列表
	var inquireBulkstockInfoPageList = function() {
		var categoryName = $("#bulkstockManage .categoryName").val();
		var bulkstockName = $("#bulkstockManage .bulkstockName").val();
		base.fnAjax({
			action: "inquireBulkstockInfoPageList.json",
			oData: {
				pageIndex: pageIndex,
				pageSize: pageSize,
				categoryName: categoryName,
				bulkstockName: bulkstockName
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					juicer.register("getValueByKey", constant.getValueByKey);
					var tpl = $("#bulkstockManageTpl").html();
					var html = juicer(tpl, data);
					$("#bulkstockManageBody").html(html);
					
					$(".bulkstockEdit").unbind("click").click(function(){
						var $this = $(this);
						var ii = layer.open({
						  title: '编辑大宗商品',
						  type: 1,
						  skin: 'layui-layer-rim', //加上边框
						  area: ['800px', '350px'], //宽高
						  content: html_edit_copy_bulkstock,
						  btn: ['确定', '取消'],
						  yes: function(){
						  	databind.submitDom($(".bulkstockInfoDialog"), function(){
						  		layer.msg("编辑成功!");
						  		layer.close(ii);
						  	});
						  }
						});
						
						$(".bulkstockInfoDialog").attr("action", "updateBulkstockInfo");
						tools.loadSelectList($(".bulkstockInfoDialog select[name=bulkstockCategoryId]"), "inquireBulkstockCategoryPageList.json", "bulkstockCategoryId", "categoryName", null);
						tools.loadSelectList($(".bulkstockInfoDialog select[name=originalPlace]"), "inquireZoneProvinceList.json", "provinceNameCN", "provinceNameCN", null);
						tools.clearNoNumAll($(".bulkstockInfoDialog"));
						
						databind.fillForm($(".bulkstockInfoDialog"), {});
						databind.fillInput1($(".bulkstockInfoDialog"), {"action": "inquireBulkstockInfo.json?bulkstockInfoId="+$this.parents("tr").attr("bulkstockInfoId")}, function(){
							
						});
					});
					
					$(".bulkstockCopy").unbind("click").click(function(){
						var $this = $(this);
						var ii = layer.open({
						  title: '复制大宗商品',
						  type: 1,
						  skin: 'layui-layer-rim', //加上边框
						  area: ['800px', '350px'], //宽高
						  content: html_edit_copy_bulkstock,
						  btn: ['确定', '取消'],
						  yes: function(){
						  	databind.submitDom($(".bulkstockInfoDialog"), function(){
						  		layer.msg("复制成功!");
						  		layer.close(ii);
						  	});
						  }
						});
						
						$(".bulkstockInfoDialog").attr("action", "createBulkstockInfo");
						tools.loadSelectList($(".bulkstockInfoDialog select[name=bulkstockCategoryId]"), "inquireBulkstockCategoryPageList.json", "bulkstockCategoryId", "categoryName", null);
						tools.loadSelectList($(".bulkstockInfoDialog select[name=originalPlace]"), "inquireZoneProvinceList.json", "provinceNameCN", "provinceNameCN", null);
						tools.clearNoNumAll($(".bulkstockInfoDialog"));
						
						databind.fillForm($(".bulkstockInfoDialog"), {});
						databind.fillInput1($(".bulkstockInfoDialog"), {"action": "inquireBulkstockInfo.json?bulkstockInfoId="+$this.parents("tr").attr("bulkstockInfoId")}, function(){
							
						});
					});
					
					$(".bulkstockSync").unbind("click").click(function(){
						var bulkstockInfoId = $(this).parents("tr").attr("bulkstockInfoId");
						layer.confirm('同步商品信息到仓储系统', {
						  btn: ['确定','取消'] //按钮
						}, function(){
						  syncCargoInfo(bulkstockInfoId);
						});
					});
					
					$(".bulkstockActive").unbind("click").click(function(){
						var bulkstockInfoId = $(this).parents("tr").attr("bulkstockInfoId");
						layer.confirm('启用商品', {
						  btn: ['确定','取消'] //按钮
						}, function(){
							activeBulkstockInfo(bulkstockInfoId);
						});
					});
					
					$(".bulkstockDeprecated").unbind("click").click(function(){
						var bulkstockInfoId = $(this).parents("tr").attr("bulkstockInfoId");
						layer.confirm('停用用商品', {
						  btn: ['确定','取消'] //按钮
						}, function(){
							deprecatedBulkstockInfo(bulkstockInfoId);
						});
					});
					
					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					base.pageControl($("#bulkstockManage .pagerCon"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							inquireBulkstockInfoPageList();
						});
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	};

	// 获取商品类别
	var inquireCommodityCategoryPageList = function() {
		var html = function(data) {
			var str = "";
			for (var i = 0; i < data.datas.length; i++) {
				var row = data.datas[i];
				str += '<option value="' + row.bulkstockCategoryId + '">' + row.categoryName + '</option>';
			}
			$("#editOrCopy_bulkstock_dialog .bulkstockCategory select").append(str);
		};
		base.fnAjax({
			action: "inquireBulkstockCategoryPageList.json", //接口名称
			async: false,
			oData: {
				activeState: true
			}, //传递参数
			fnSuccess: function(data) { //成功回调
				if (data.status == 200) {
					html(data);
				} else {
					base.alert(3, data.message);
				}
			}
		});
	};
	
	// 同步商品信息到仓储系统
	var syncCargoInfo = function(bulkstockInfoId) {
		base.fnAjax({
			action: "syncCargoInfo.json", //接口名称
			oData: {
				bulkstockInfoId: bulkstockInfoId
			}, //传递参数
			fnSuccess: function(data) { //成功回调
				if (data.status == 200) {
					base.alert("1", "同步成功！");
					router.route("bulkstockManage/bulkstockManage.html");
				} else {
					base.alert(3, data.message);
				}
			}
		});
	};
	
	// 启用商品
	var activeBulkstockInfo = function(bulkstockInfoId) {
		base.fnAjax({
			action: "activeBulkstockInfo.json", //接口名称
			oData: {
				bulkstockInfoId: bulkstockInfoId
			}, //传递参数
			fnSuccess: function(data) { //成功回调
				if (data.status == 200) {
					base.alert("1", "启用成功！");
					router.route("bulkstockManage/bulkstockManage.html");
				} else {
					base.alert(3, data.message);
				}
			}
		});
	};

	// 停用商品
	var deprecatedBulkstockInfo = function(bulkstockInfoId) {
		base.fnAjax({
			action: "deprecatedBulkstockInfo.json", //接口名称
			oData: {
				bulkstockInfoId: bulkstockInfoId
			}, //传递参数
			fnSuccess: function(data) { //成功回调
				if (data.status == 200) {
					base.alert("1", "停用成功！");
					router.route("bulkstockManage/bulkstockManage.html");
				} else {
					base.alert(3, data.message);
				}
			}
		});
	};

	//继续添加材料模板
	var addMaterials = function() {
		var str = [
			'  <div style="margin-top: 16px;" class="materials">',
			'  <div style="display: inline-block">',
			'    <label for="" class="newsTitle">新增所需材料', , '：</label>',
			'    <input class="material" type="text" id="templateName" placeholder="例如：商品质检证明" />',
			'  </div>',
			'  <form style="display: inline-block">',
			'    <label for="" class="isUpload">是否必须上传：</label>',
			'	 <input type="radio" class="checkedRequired" name="checkedRequired" value="1" style="width: auto; height: auto;" />是',
			'    <input type="radio" class="checkedRequired" name="checkedRequired" value="0" checked="checked" style="width: auto; height: auto; margin-left: 20px;" />否',
			'    <span class="table_bar">| </span>',
			'    <span class="delete">删除 </span>',
			'  </form>',
			'  </div>',
		].join("");
		i++;
		$('.hr').before(str);
	};

	// 判断搜索框是否都为空
	var ifblank = function() {
		if (($("#categoryName")).val() == "") {
			$("#commodityCategoryId").val("");
		}
	};


	//下拉补全判断方法
	var changeFunction = function(idValue, idhidden, url) {
		if ($(idValue).val().length == 0) {
			$(idValue).val("");
			$(idhidden).val("");
			return;
		};
		var keyword = $(idValue).val();
		keyword = keyword.replace(/[ ]/g, "");
		if (keyword == "") {
			return;
		};
		base.fnAjax({
			action: url,
			nocode: true,
			oData: {
				keyword: keyword,
				pageSize: 50
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					if (data.datas.length > 0) {
						if (data.datas[0].commodityCategoryId > 0) {
							$(idValue).val(data.datas[0].categoryName);
							$(idhidden).val(data.datas[0].commodityCategoryId);
						} else {
							$(idValue).val(data.datas[0].categoryName);
							$(idhidden).val(data.datas[0].commodityCategoryId);
						};
					} else {
						$(idhidden).val("");
					};
				} else {
					$(idhidden).val("");
				}
			},
			error: function() {
				$(idhidden).val("");
			}
		});
	};



	return {
		init: init
	};
});